<script setup lang="ts">
import useMainStore from '@/store/main/main'
import useRoleStore from '@/store/role/role'
import { mapMenuId } from '@/utils/mapMenuid'
import type { ElTree } from 'element-plus'

import { storeToRefs } from 'pinia'
import { nextTick, reactive, ref } from 'vue'

const mainStore = useMainStore()
const roleStore = useRoleStore()

// 1.定义内部的属性
const formData = reactive<any>({
  name: '',
  intro: '',
  menuList: []
})

// 2.获取部门及角色数据
const { menuList } = storeToRefs(mainStore)
// 3.显示弹窗的方法
const centerDialogVisible = ref(false)
// 显示密码框
const isShowPsW = ref(true)
// 编辑角色的信息
const treeRef = ref<InstanceType<typeof ElTree>>()
const editData = ref<any>()
const showModel = (isNew: boolean = true, itemData?: any) => {
  // 1.显示弹窗
  centerDialogVisible.value = true

  // 2.是否显示密码框
  isShowPsW.value = isNew

  if (!isNew && itemData) {
    //当isNew等于false并且itemData有值就是编辑角色

    // 3.编辑角色
    for (const key in formData) {
      formData[key] = itemData[key]
      // 4.存储编辑角色的信息
      editData.value = itemData
    }

    const menuList = mapMenuId(itemData.menuList)
    formData.menuList = menuList

    // 初始化时绑定数据
    nextTick(() => {
      treeRef.value?.setCheckedKeys(formData.menuList)
    })
  } else {
    // 5.新建角色
    for (const key in formData) {
      formData[key] = ''
      // 重置树形选中的标签
      treeRef.value?.setCheckedKeys([])
    }
  }
}

const handleMenuCheckChange = (data1: any, data2: any) => {
  console.log(data2.checkedKeys)

  formData.menuList = [...data2.checkedKeys, ...data2.halfCheckedKeys]
}

// 4.确定逻辑
const handleConfirmClick = () => {
  // 关闭弹窗
  centerDialogVisible.value = false

  if (!isShowPsW.value && editData.value) {
    // 编辑角色
    const id = editData.value.id
    roleStore.editRoleDataAction(id, formData)
  } else {
    // 新建角色
    roleStore.createRoleDataAction(formData)
  }
}

// 暴露事件
defineExpose({ showModel })
</script>

<template>
  <div class="user-model">
    <el-dialog
      v-model="centerDialogVisible"
      :title="isShowPsW ? '新增角色' : '编辑角色'"
      width="30%"
      center
    >
      <el-form :model="formData" label-width="80px" size="large">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="formData.name" placeholder="请输入角色名称" />
        </el-form-item>
        <el-form-item label="权限介绍" prop="intro">
          <el-input v-model="formData.intro" placeholder="请输入权限介绍" />
        </el-form-item>
        <el-tree
          ref="treeRef"
          :data="menuList"
          show-checkbox
          node-key="id"
          highlight-current
          :props="{ label: 'name', children: 'children' }"
          @check="handleMenuCheckChange"
        />
      </el-form>

      <template #footer>
        <span class="dialog-footer">
          <el-button @click="centerDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleConfirmClick"> 确定 </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<style lang="less" scoped>
.user-model {
}
</style>
